package com.lu.sb210207.controller;


import com.lu.sb210207.entity.ScheduleTrigger;
import com.lu.sb210207.result.JsonResult;
import com.lu.sb210207.service.SchedulerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("quartz")
public class ScheduleController {

    @Autowired
    private SchedulerService schedulerService;

    /**
     * 添加定时任务
     * @param scheduleTrigger
     * @return
     */
    @PostMapping("add")
    public Object addJob(ScheduleTrigger scheduleTrigger) {

        schedulerService.addJob(scheduleTrigger);

        return JsonResult.builder().code(200).msg("定时任务添加成功！").build();
    }

    /**
     * 停止定时任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @PostMapping("pause")
    public Object pause(String jobName, String jobGroup) {

        schedulerService.jobPause(jobName, jobGroup);

        return JsonResult.builder().code(200).msg("定时任务暂停成功！").build();
    }

    /**
     * 开始定时任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @PostMapping("resume")
    public Object resume(String jobName, String jobGroup) {

        schedulerService.jobResume(jobName, jobGroup);

        return JsonResult.builder().code(200).msg("定时任务开始成功！").build();
    }

    /**
     * 删除定时任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @PostMapping("delete")
    public Object delete(String jobName, String jobGroup) {

        schedulerService.jobdelete(jobName, jobGroup);

        return JsonResult.builder().code(200).msg("定时任务删除成功！").build();
    }

    /**
     * 更新定时任务
     * @param scheduleTrigger
     * @return
     */
    @PostMapping("update")
    public Object update(ScheduleTrigger scheduleTrigger) {

        schedulerService.updateJob(scheduleTrigger);

        return JsonResult.builder().code(200).msg("定时任务更新成功！").build();
    }

}
